package hxg.com;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author HeXiangGuang
 * @create 2021-03-31 22:13
 */
public class Test {
    public static void main(String[] args) {
        //创建固定个数的线程池
            ExecutorService es = Executors.newFixedThreadPool(4);
            //创建缓存线程池，线程个数由任务个数决定
//            ExecutorService es = Executors.newCachedThreadPool();
            Runnable runnable = new Runnable() {
                private int ticket = 100;
                @Override
                public void run() {
                    while (true){
                        if (ticket<=0){
                            break;
                        }
                        System.out.println(Thread.currentThread().getName()+"买了第"+ticket+"张票");
                        ticket--;
                    }
                }
            };
            //提交任务
        for (int i = 0; i < 4 ; i++) {
            es.submit(runnable);
        }
        //关闭线程池
        es.shutdown();//等到所有线程结束然后在关闭线程
    }
}
